﻿@model RH.Web.ViewModels.UtilityModels.ListConfig
@using RH.Common.Extensions;
@using RH.Web.ViewModels.UtilityModels;
@using RH.Web.ViewModels;
@using RH.Web.Utility;
@{
    var _SearchModel = ((RH.Web.ViewModels.SavedSearchViewModel)Model.AdhokModel);
    String keyword = _SearchModel.Keyword;
    String pageTitle = "Property Search Result";
    ViewBag.Title = Html.GetPageTitle(pageTitle);
    //ListConfig _ListConfig = Model;
    PagerConfig _PagerConfig = Model.Pager;
    ViewBag.DataSource = Model.DataSource;
    ViewBag.TotalRecord = Model.Pager.TotalRecord;
    String type = _SearchModel.IsForRent ? "R" : "B"; //WebUtil.GetQueryStringValue("type");
    //String location = WebUtil.GetQueryStringValue("loc");
    _PagerConfig.UrlFormat = "/product/search?key=" + keyword + "&pageNo={0}&type=" + type ;//+ "&loc=" + (_SearchModel.LoactionID > 0) ? _SearchModel.LoactionID.ToString() : "";
    if (keyword.IsNullOrEmpty())
    {
        ViewBag.ShowMessage = false;
    }
    ViewData[AppConstants.ViewDataKeys.CALCULATE_OCCURANCE] = true;
    //int _CategoryID =  //WebUtil.GetQueryStringValue("Cat").ToInt();
    String _SearchType = _SearchModel.IsForRent ? "R" : "B";;
    String _CurentUrl = Request.RawUrl;
}

<div class="rh-page-heading">
    <h1>@pageTitle</h1>
    <div>
        @if(keyword.IsNullOrEmpty()){
            <text>Enter your keyword in the following text box and click Search.</text>
        }
        else {
        <text>List of matched properties(s) for your given search keyword.</text>
        }
    </div>
</div>
                    
<div class="rh-page-content">
    @using (Html.BeginForm("search", "product", FormMethod.Post, new { id="frmSearch"}))
    {
        <div>
            <div class="input-group">
                <input class="form-control" id="txtKeyword" name="keyword" type="text" maxlength="100" value="@keyword" placeholder="Enter your search criteria" />
                <div class="input-group-btn pull-left">
                    <button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i> Search</button>                    
                </div>
                <div class="input-group-btn">
                    <button class="btn btn-default" type="button" id="btnSearchTips"><i class="glyphicon glyphicon-lamp"></i> Search Tips</button>
                </div>
                <input type="hidden" id="hdnSearchType" name="searchType" value="@_SearchType" />
            </div>
            <div class="clearfix"></div>
            @{
                String buyCss = "primary";
                String rentCss = "default";
                if (String.Compare(type, "R", true) == 0)
                {
                    buyCss = "default";
                    rentCss = "primary";
                }
            }
            <div>
                <div class="input-group rh-top-5px">
                    <div class="input-group-btn">
                        <button class="btn btn-@buyCss" type="button" name="type" value="B"><i class="glyphicon glyphicon-shopping-cart"></i> Buy</button>
                        <button class="btn btn-@rentCss" type="button" name="type" value="R"><i class="glyphicon glyphicon-book"></i> Rent</button>
                    </div>
                    @*@Html.DropDownListFor(model => ((SavedSearchViewModel)model.AdhokModel).CategoryID, BindSelectList.Categories("Select Property Type"))*@
                    
                    @Html.DropDownList("CategoryID", BindSelectList.Categories(_SearchModel.CategoryID))
                </div>
            </div>
            @*<div class="input-group">
                <input class="btn btn-primary" id="txtKeyword" name="keyword" type="text" maxlength="100" value="@keyword" placeholder="Enter your search criteria" />
                <div class="input-group-btn pull-left">
                    <button class="btn btn-default" type="button" onclick="SubmitSearchForm();"><i class="glyphicon glyphicon-search"></i> Search</button>
                </div>
                <div class="input-group-btn">
                    <button class="btn btn-default" type="button" id="btnSearchTips"><i class="glyphicon glyphicon-lamp"></i> Search Tips</button>
                </div>

                <div class="clearfix"></div>
            </div>*@
        </div>
    }

    <div id="divSearchResult" class="rh-top-15px">
        <div class="panel panel-success@{if(keyword.IsNullOrEmpty() && Model.Pager.TotalRecord == 0) {<text> hidden</text>}}">
            <div class="panel-heading">
                <div class="pull-left"><h4>Property Search Result</h4></div>
                <div class="pull-right">
                    @if (Model.Pager.TotalRecord > 0) { 
                        <button class="btn btn-success" onclick="SaveSearch(this);"><i class="glyphicon glyphicon-floppy-save"></i> Save this Search</button>
                    }
                </div>
                <div class="clearfix"></div>
            </div>
            <div class="panel-body">
                @{Model.AdhokModel = false;}
                @Html.Partial("_ProductList")
            </div>
        </div>
        @if (_PagerConfig != null && !keyword.IsNullOrEmpty())
        {    
            @Html.Partial("_Pager", _PagerConfig)
        }
    </div>
</div>

@section RightSideBar{
<div class="right-sidebar-container">
    <div style="border-bottom:#EFEFEF 1px solid; padding-bottom:2px; margin-bottom:10px;">
        <h2>Narrow Result</h2>
    </div>
    <div id="divSearchFilters"></div>
</div>
}
<div id="divSearchTips" class="hidden">
    <a href="/">@ConfigReader.SiteName</a>'s search engine supports several techniques of search. 
    You can specify them in your search keyword. They are preety simple and you can try few of them. 
    Suppose you are looking for a Digital Camera, then you can format your keywords in following ways:
    <ul class="search-tips">
        <li>
            If you want a exact match in your search result then sorrund your keyword with a <b>double quote</b> (")<br />
            <b>Example:</b> "Digital Camera"
        </li>
        <li>
            If you want to match all of your keywords, then you can use <b>and</b> operator.<br />
            <b>Example:</b> Digital AND Camera
        </li>
        <li>
            If you want to match few of your keywords, then you can try <b>or</b> operator.<br />
            <b>Example:</b> Digital OR Camera
        </li>
        <li>
            Or you can simply enter your search criteria about what you are looking for and 
            @ConfigReader.SiteName's search engine will intelligently show matching products.
        </li>
    </ul>
</div>
@*Delete Dialog Modal Start *@
@{
    RH.Web.ViewModels.ModalViewModel searchModal = new RH.Web.ViewModels.ModalViewModel("searchDialog", "Search Tips", String.Empty);
    RH.Web.ViewModels.ModalViewModel messageModal = new RH.Web.ViewModels.ModalViewModel("messageDialog", "Confirmation", String.Empty);
    //messageModal.SaveButton = String.Empty;
}
@Html.Partial("_Modal", searchModal)
@Html.Partial("_Modal", messageModal)
@*Delete Dialog Modal End *@


@section PageHeadSection
{
    <style type="text/css">
        #txtKeyword{/*float:left;*/ font-size:19px; font-weight:normal; width:400px;}
        /*.search-box{margin:0px 0px 15px 3px;}*/
        /*.search-tips{margin:10px 0px 5px 0px;} .search-tips li{margin-top:7px;}*/
        #divSearchResult{}
        #CategoryID{width:220px; margin-left:40px;}
        @if(Model.Pager.TotalRecord > 0){
            <text>.panel-body{padding-top:0px; padding-bottom:0px;}</text>
        }
    </style>
}

@section JavaScriptPlaceholder{
    @if (ConfigReader.IsProductionDeployment) {
        @Html.CDNValidate()
    } else {
        @Scripts.Render(Bundles.jQueryValidate)
    }
    
    <script language="javascript" type="text/javascript">
        var _SavedSearch = @MvcHtmlString.Create(_SearchModel.ToJSON());
        var _SearchTipDialog = null;
        $(document).ready(function () {
            $('#btnSearchTips').click(function () {
                $('#searchDialog').modal('show').find('.modal-body').html($('#divSearchTips').html());
            });
            $('[name="type"]').each(function () {
                $(this).click(function () {
                    var hasValueChanged = $(this).val() != $('#hdnSearchType').val() ? true : false;
                    $('#hdnSearchType').val($(this).val());
                    $('[name="type"]').removeClass('btn-primary').addClass('btn-default');
                    $(this).removeClass('btn-default').addClass('btn-primary');
                    if(hasValueChanged)
                        $('#divSearchResult').slideUp();
                });
            });
        });
        
        function SaveSearch(btn) {
            //var messabeBox= $('#messageDialog');
            //SetModalBody(messabeBox, "This is test HTML");
            //$(messabeBox).modal('show').find('.modal-body');
            ShowLoadingAnimation(false);
            Ajax('@Url.PublicUrl("SaveSearch", "Ajax")', _SavedSearch, function(result){
                var html = '', messageBox= $('#messageDialog'),
                    loginUrl = '@Url.PublicUrl("logon", "account")?ReturnUrl=@Request.RawUrl.UrlEncode()&search=' + encodeURIComponent(JSON.stringify(_SavedSearch));
                if(result < 0 )
                    html = WarningMessageBox('Please <a href="' + loginUrl + '">login</a> to save this search result.', 'Login Required!');
                else{
                    html = SuccessMessageBox('This search result has been saved in your account, you can review them any time from your <a href="/Private/Account/Dashboard">dashboard</a>', '');
                    $(btn).hide();
                }
                //$(btn).prop('href', loginUrl);
                $(messageBox).modal('show');
                SetModalBody(messageBox, html);
                HideLoadingAnimation();
            });
        }
        ///**** Search Filter Functionality Starts from Here  ***/
        function SearchPayload(keyword) {
            this.Keyword = keyword;
            this.Filters = [];
        }
        $(function () {
            PopulateSearchFilters();
        });
        function PopulateSearchFilters() {
            var catID = $('#hdnCategoryID').val();
            if (catID > 0) {
                AjaxGET('/search/BuildFilters?categoryID=' + catID, function (result) {
                    if (result.length > 0) {
                        $('#divSearchFilters').html(result);
                    }
                    $('#divSearchFilters').find('input').click(function () {
                        RefreshSearchResult($(this));
                    });
                });
            }
        }
        function ClearFilters() {
            $('.search-filters input').each(function () {
                $(this).attr('checked', false);
            });
            RefreshSearchResult(null);
        }
        function RefreshSearchResult(fElement) {
            var keyword = $('#txtKeyword').val();
            if (keyword.length > 0) {
                ShowLoadingAnimation(false);
                var payload = new SearchPayload(keyword);
                $('.search-filters input').each(function (obj, i) {
                    if ($(this).is(':checked')) {
                        var searchType = $(this).attr('type') == 'radio' ? 'Range' : 'MultiSelect';
                        var filter = { SearchType: searchType, SearchField: $(this).parent().parent().attr('searchfield'), Value: $(this).val() };
                        payload.Filters.push(filter);
                    }
                });
                Ajax('/search/RefineSearch', 'json=' + JSON.stringify(payload), function (result) {
                    HideLoadingAnimation();
                    $('#divSearchResult').html(result);
                    //alert(result);
                });
            }
            //alert($(fElement).attr('value'));
        }
        ///*** End of Search Filter Functionality
    </script>
    @*@Html.Partial("_AutocompleteJS")*@
}